package com.nanhang.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.nanhang.entity.TbSpecificationOption;
import com.nanhang.entity.TbSpecificationOptionExample;
import com.nanhang.mapper.TbSpecificationOptionMapper;
import com.nanhang.service.TbSpecificationOptionService;
import com.nanhang.util.PageResult;
import com.nanhang.vo.TbSpecificationOptionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description
 * @Author minguue
 * @Date 2025/1/5
 */

@Service
public class TbSpecificationOptionServiceImpl implements TbSpecificationOptionService {

    @Autowired
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;
    @Override
    public PageResult<TbSpecificationOption> search(Integer page, Integer pageSize, TbSpecificationOptionVO tbSpecificationOptionVO) {
        // 1. 开始分页
        PageHelper.startPage(page, pageSize);
        // 2. 查询数据
        // 2.1 定义一次查询的实例
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        // 2.2 添加查询条件到查询实例中
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        if(StrUtil.isNotBlank(tbSpecificationOptionVO.getOptionName())){
            criteria.andOptionNameLike("%" + tbSpecificationOptionVO.getOptionName() + "%");
        }

        if(tbSpecificationOptionVO.getSpecId() != null && tbSpecificationOptionVO.getSpecId() != 0){
            criteria.andSpecIdEqualTo(tbSpecificationOptionVO.getSpecId());
        }
        List<TbSpecificationOption> tbSpecificationOptions = tbSpecificationOptionMapper.selectByExample(example);


        // 2.4 将上面的students对象转换为List集合
        Page<TbSpecificationOption> tbSpecificationOptionsPage = (Page<TbSpecificationOption>) tbSpecificationOptions;
        return new PageResult<>(tbSpecificationOptionsPage.getResult(),tbSpecificationOptionsPage.getTotal());
    }

    @Override
    public boolean save(TbSpecificationOption tbSpecificationOption) {
        return tbSpecificationOptionMapper.insert(tbSpecificationOption) > 0;
    }
}
